package cn.alm.allinoneboot.controller;

import cn.alm.allinoneboot.common.CommonResult;
import cn.alm.allinoneboot.dto.CommonDto;
import cn.alm.allinoneboot.entity.Brand;
import cn.alm.allinoneboot.service.ApiService;
import cn.alm.allinoneboot.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/brand")
@CrossOrigin(origins = "*", maxAge = 3600)
@Slf4j
public class BrandController {

    @Resource
    ApiService apiService;

    @Resource
    BrandService brandService;

    // 新增品牌
    @PostMapping("add")
    public CommonResult addBrand(@RequestHeader(value = "authorization", required = false) String authorization, @RequestBody Brand brand) {
        if (!apiService.tokenValid(authorization)) {
            CommonResult cr = new CommonResult();
            cr.unAuth();
            return cr;
        }
        return brandService.addBrand(brand);
    }

    // 根据品牌id查询品牌
    @PostMapping("getBrandById")
    public CommonResult getBrandById(@RequestHeader(value = "authorization", required = false) String authorization, @RequestBody Brand brand) {
        if (!apiService.tokenValid(authorization)) {
            CommonResult cr = new CommonResult();
            cr.unAuth();
            return cr;
        }
        return brandService.getBrandById(brand);
    }

    // 根据品牌id删除品牌
    @PostMapping("deleteBrandById")
    public CommonResult deleteBrandById(@RequestHeader(value = "authorization", required = false) String authorization, @RequestBody Brand brand) {
        if (!apiService.tokenValid(authorization)) {
            CommonResult cr = new CommonResult();
            cr.unAuth();
            return cr;
        }
        return brandService.deleteBrandById(brand);
    }

    // 更新品牌
    @PostMapping("update")
    public CommonResult updateBrand(@RequestHeader(value = "authorization", required = false) String authorization, @RequestBody Brand brand) {
        if (!apiService.tokenValid(authorization)) {
            CommonResult cr = new CommonResult();
            cr.unAuth();
            return cr;
        }
        return brandService.updateBrand(brand);
    }

    // 获取品牌列表分页
    @PostMapping("queryBrandListPage")
    public CommonResult queryBrandListPage(@RequestHeader(value = "authorization", required = false) String authorization, @RequestBody CommonDto dto) {
        if (!apiService.tokenValid(authorization)) {
            CommonResult cr = new CommonResult();
            cr.unAuth();
            return cr;
        }
        return brandService.queryBrandListPage(dto.getName(), dto.getCurrent(), dto.getSize());
    }

    // 获取所有品牌
    @PostMapping("getAllBrandList")
    public CommonResult getAllBrandList(@RequestHeader(value = "authorization", required = false) String authorization) {
        if (!apiService.tokenValid(authorization)) {
            CommonResult cr = new CommonResult();
            cr.unAuth();
            return cr;
        }
        List<Brand> allBrandList = brandService.getAllBrandList();
        CommonResult cr = new CommonResult();
        cr.ok();
        cr.setData(allBrandList);
        return cr;
    }
}
